package cn.kgc.tangcco.tcbd1011.no13.vehiclebrand.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


import cn.kgc.tangcco.tcbd1011.no13.base.util.JDBCUtils;
import cn.kgc.tangcco.tcbd1011.no13.vehiclebrand.dao.VehicleBrandDao;
import cn.kgc.tangcco.tcbd1011.no13.vehiclebrand.entity.VehicleBrand;

public class VehicleBrandDaoImpl implements VehicleBrandDao {

	private QueryRunner queryRunner;
	public VehicleBrandDaoImpl() {
		this.queryRunner = new QueryRunner(JDBCUtils.getDataSource());
	}
	//添加页面
	@Override
	public int addVehicleBrand(VehicleBrand vehiclebrand) {
		try {
			return this.queryRunner.update("insert into vehiclebrand value(default,?)",vehiclebrand.getVehicleBrand());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	//修改
	@Override
	public int updateVehicleBrand(VehicleBrand vehiclebrand) {
		try {
			return this.queryRunner.update("update vehiclebrand set vehicleBrand=? where vehicleBrandId=?",vehiclebrand.getVehicleBrand(),vehiclebrand.getVehicleBrandId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	//删除
	@Override
	public int deleteVehicleBrand(int pk) {
		QueryRunner qr = new QueryRunner();
		Connection conn = null;
		try {
			conn = JDBCUtils.getConnection();
			//关闭自动提交
			conn.setAutoCommit(false);
			//设置事务的隔离级别
			conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
			//具体的操作
			/**
			 * 1、先删除车辆表中属于这个品牌的所有车辆
			 * 2、再删除这个品牌下的所有车型
			 * 3、最后删除这个品牌
			 * 
			 */
			String delete_vehicle_sql = "delete  from vehicle where vehicleTypeId in(select vehicleTypeId from vehicleType where vehicleBrandId=?)";
			qr.update(conn, delete_vehicle_sql, pk);
			String delete_vehicleType_sql="delete from vehicleType where vehicleBrandId=?";
			qr.update(conn,delete_vehicleType_sql,pk);
			String delete_vehicleBrand_dql = "delete from vehicleBrand where vehicleBrandId=?";
			qr.update(conn, delete_vehicleBrand_dql, pk);
			//同时成功
			conn.commit();
			return 1;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<VehicleBrand> queryAllVehicleBrand() {
		try {
			return this.queryRunner.query("select * from vehiclebrand order by vehicleBrandId desc", new BeanListHandler<>(VehicleBrand.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public VehicleBrand detailVehicleBrand(int pk) {
		try {
			return this.queryRunner.query("select * from vehiclebrand where vehicleBrandId=?", new BeanHandler<>(VehicleBrand.class),pk);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long queryCountVehicleBrand() {
		try {
			return ((Long)this.queryRunner.query("select count(*) from vehiclebrand ", new ScalarHandler<>())).intValue();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public List<VehicleBrand> queryAllToVehicletype() {
		try {
			return this.queryRunner.query("select * from vehiclebrand order by vehicleBrandId desc", new BeanListHandler<>(VehicleBrand.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<VehicleBrand> queryAllToVehicle() {
		try {
			return this.queryRunner.query("select * from vehiclebrand order by vehicleBrandId desc", new BeanListHandler<>(VehicleBrand.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
